<?php
# Demo for using the RATS PHP Client Library (based on the Zend GData libraries).
# This demo only demonstrates uploading a src file and using it to request a transcoding job, as well as retrieving some feeds and entries.
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once '../RawsClient/Raws/Rats.php';
# transient states -> processing not complete
define("REQUEST_RECEIVED", 1);
define("IMPORT_IN_PROGRESS", 2);
define("IMPORT_SUCCEEDED", 3);
define("TRANS_IN_PROGRESS", 4);
define("TRANS_SUCCEEDED", 5);
define("EXPORT_IN_PROGRESS", 6);
# final states
define("FINISHED", 7);
define("IMPORT_FAILED", 8);
define("TRANS_FAILED", 9);
define("EXPORT_FAILED", 10);
# fixed data
define('BASE', 'http://rats.enc01.rambla.be/');
define('SRC_FILENAME', 'test_one_srcfile.mp4'); # src_location
define('TGT_FILENAME', 'test_one.mp4'); # tgt_location

# Provide your own credentials here
define('USER', 'xxx');
define('PWD', 'xxx');
# provide a path to your local file to be used as transcoding src
define('LOCAL_FILE', '../test_resources/bla.mp4');
# you should specify your transcoding format ID here
define('FORMAT_PROFILE', ''); # E.g. "12"
# if you want to use an existing input profile, you should fill in its ID here, otherwise leave blank
define('INPUT_PROFILE', ''); # E.g. "14"
# if you want to use an existing output profile, you should fill in its ID here, otherwise leave blank
define('OUTPUT_PROFILE', ''); # E.g. "14"


# Note that all REST methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  # Create a Rats instance, passing it your login credentials and the base service uri
  $rats = new Rats(USER, PWD, BASE);
  
  # upload a src file and create a src instance
  $src_entry = $rats->createSrc(SRC_FILENAME, LOCAL_FILE);
  echo "\nUploaded a src file: \n";
  echo "Location of uploaded src: " . $src_entry->id->text . "\n";
  echo "Filename on the RATS svr: " . $src_entry->content->params->filename->text . "\n";
  echo "File will be removed: " . $src_entry->content->params->to_be_removed->text . "\n";
    
  # use the entry's id, to retrieve the entry once more
  $src_entry = $rats->getSrcEntry($src_entry->id->text);
  echo "\nRetrieving a src entry:\n";
  echo "Retrieved entry: " . $src_entry->id->text . "\n";
  
  # the filename can also be used to retrieve the entry
  $src_entry = $rats->getSrcEntryFromFilename($src_entry->content->params->filename->text);
  echo "\nRetrieving a src entry, using its filename:\n";
  echo "Retrieved entry from filename: " . $src_entry->content->params->filename->text . "\n";
  
  # retrieve the feed of all srcs for this user
  $src_feed = $rats->getSrcFeed();
  echo "\nRetrieving a feed of all existing src entries:\n";
  foreach ($src_feed as $src_entry) {
    echo "---\n";
    echo "location of the src: " . $src_entry->id->text . "\n";
    echo "filename: " . $src_entry->content->params->filename->text . "\n";
    echo "to be removed: " . $src_entry->content->params->to_be_removed->text . "\n";
  }

  # create a new Job, using fixed format profile
  $job_entry = $rats->createJobForExistingSrc(OUTPUT_PROFILE, FORMAT_PROFILE, $src_entry->content->params->filename->text, TGT_FILENAME);
  $job_id = $job_entry->content->params->id->text;
  echo "\nCreating a job to transcode the job:\n";
  echo "job ID: " . $job_id . "\n";
  echo "job Status: " . $job_entry->content->params->status->text . "\n";
  echo "job's format ID: " . $job_entry->content->params->format->text . "\n";

  # find out if it's there
  $job_entry = $rats->getJobEntry($job_entry->id->text);
  echo "\nRetrieving the job entry from the server:\n";
  echo "job ID: " . $job_entry->content->params->id->text . "\n";
  echo "job Status: " . $job_entry->content->params->status->text . "\n";
  echo "job's format ID: " . $job_entry->content->params->format->text . "\n";
  # for a better understanding, you may want to take look at the xml
  echo "\nComplete job entry in xml format:\n" . $job_entry->getXML() . "\n\n";
  
  # get a feed of all jobs
  $job_feed = $rats->getJobFeed();
  echo "\nRetrieving a feed of all existing job entries:\n";
  foreach ($job_feed as $job_entry) {
    echo "---\n";
    echo "location of the job: " . $job_entry->id->text . "\n";
    echo "ID: " . $job_entry->content->params->id->text . "\n";
    echo "status: " . $job_entry->content->params->status->text . "\n";
  }

}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}